#pragma once

#include "macinteo.h"

bool Macinteo::Init(Register& o_register)
{
	m_name = "Matteo Salsilli";

	REGISTER_FUNC(Macinteo, 1);
	REGISTER_FUNC(Macinteo, 2);
	REGISTER_FUNC(Macinteo, 3);

	return true;
}

RetType Macinteo::Problem1()
{
	/**
	 *  Add all the natural numbers below one thousand
	 *  that are multiples of 3 or 5.
	 */
	RetType max=0;
	for (int i=0; i<1000; i++)
		max += ((i%3==0) || (i%5==0)) ? i : 0;

	return max; // 233168
}

RetType Macinteo::Problem2()
{
	/**
	 *  Find the sum of all the even-valued terms
	 *  in the Fibonacci sequence which do not exceed
	 *  four million.
	 */
	int the_solution = 0;
	int the_fib = 1;
	int fib1 = 0;
	int previous_fib = 1;

	while (the_fib<4000000) {
		previous_fib = the_fib;
		the_fib += fib1;
		fib1 = previous_fib;
		if ( (the_fib%2) == 0 )
			the_solution += the_fib;

	}

	return the_solution;
}


RetType Macinteo::Problem3()
{
	/**
	 *  Find the largest prime factor of a
	 *  composite number.
	 */
	UInt64 the_puzzle = 600851475143;
	UInt64 max_prime_divisor = 1;

	for ( UInt64 i=3; i<300851475143; i += 2)
	{
		if ( (the_puzzle % i) == 0)
			the_puzzle = the_puzzle / i;
			if (i>max_prime_divisor)
				max_prime_divisor = i;
	}

	return max_prime_divisor;
}